package com.cml.common.serializable;

import com.cml.common.dto.entity.Message;
import com.cml.common.dto.message.ServiceHearbeatRequest;
import com.cml.common.util.SocketChannelParseStringUtil;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 姓名：陈茂林
 * 时间：2023/11/6 13:11
 * 描述：
 */
public class ServiceHearbeatRequstSerializable extends MessageSerializable {
    public void doBuildEncoderMessage(Message message, ByteBuffer byteBuffer) {
        ServiceHearbeatRequest serviceRegisterRequest = (ServiceHearbeatRequest) message;
        SocketChannelParseStringUtil.stringDataBuild(byteBuffer,serviceRegisterRequest.getServiceName());
    }

    public Message doBuildDecoderMessage(SocketChannel socketChannel) throws IOException {
        ServiceHearbeatRequest serviceRegisterRequest = new ServiceHearbeatRequest();

        serviceRegisterRequest.setServiceName(SocketChannelParseStringUtil.parse(socketChannel));

        InetSocketAddress inetSocketAddress = (InetSocketAddress) socketChannel.getRemoteAddress();
        serviceRegisterRequest.setIp(inetSocketAddress.getAddress().getHostAddress());
        serviceRegisterRequest.setPort(inetSocketAddress.getPort());
        return serviceRegisterRequest;
    }
}
